home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / windownt / mentc.zip / NTCON.C < prev    next >
C/C++ Source or Header  |  1993-04-18  |  9KB  |  429 lines

  1. //
  2. //    Windows NT Console support for MicroEMACS 3.xx
  3. //
  4. //    April 18th, 1993 - by J. Oklamcak
  5. //
  6.  
  7. #include <windows.h>
  8.  
  9. #define termdef 1
  10.  
  11. #include <stdio.h>
  12. #include "estruct.h"
  13. #include "eproto.h"
  14. #include "edef.h"
  15. #include "elang.h"
  16.  
  17. #if NTCON
  18.  
  19. #define NUMCOL 80    // Current columns
  20. #define NUMROW 25    // Current rows
  21.  
  22. #define MAXCOL 256    // Maximum columns
  23. #define MAXROW 256    // Maximum rows
  24.  
  25. #define    MARGIN 8    // size of minimim margin and
  26. #define    SCRSIZ 64    // scroll size for extended lines
  27. #define    NPAUSE 100    // # times thru update to pause
  28.  
  29. #define OBUFSIZE 512    // Output buffer size
  30.  
  31. //
  32. //    Function prototypes
  33. //
  34.  
  35. VOID PASCAL NEAR ttopen(VOID);
  36. VOID PASCAL NEAR ttclose(VOID);
  37. VOID PASCAL NEAR ttkopen(VOID);
  38. VOID PASCAL NEAR ttkclose(VOID);
  39. int PASCAL NEAR ttgetc(VOID);
  40. VOID PASCAL NEAR ttputc(int);
  41. VOID PASCAL NEAR ttflush(VOID);
  42. VOID PASCAL NEAR ttmove(int, int);
  43. VOID PASCAL NEAR tteeol(VOID);
  44. VOID PASCAL NEAR tteeop(VOID);
  45. VOID PASCAL NEAR ttbeep(VOID);
  46. VOID PASCAL NEAR ttrev(int);
  47. int PASCAL NEAR ttrez(int);
  48. #if COLOR
  49. VOID PASCAL NEAR ttfcol(int);
  50. VOID PASCAL NEAR ttbcol(int);
  51. #endif
  52.  
  53. //
  54. //    Dispatch table for console I/O
  55. //
  56.  
  57. TERM term =
  58. {
  59.     MAXROW-1,
  60.     NUMROW-1,
  61.     MAXCOL,
  62.     NUMCOL,
  63.     0, 0,
  64.     MARGIN,
  65.     SCRSIZ,
  66.     NPAUSE,
  67.     &ttopen,
  68.     &ttclose,
  69.     &ttkopen,
  70.     &ttkclose,
  71.     &ttgetc,
  72.     &ttputc,
  73.     &ttflush,
  74.     &ttmove,
  75.     &tteeol,
  76.     &tteeop,
  77.     &tteeop,
  78.     &ttbeep,
  79.     &ttrev,
  80.     &ttrez,
  81. #if COLOR
  82.     &ttfcol,
  83.     &ttbcol,
  84. #endif
  85. };
  86.  
  87. #define KEYTBLSIZE 22    // Keyboard table size
  88.  
  89. static WORD KeyTbl[KEYTBLSIZE][3] =
  90. {
  91.     {VK_LEFT,    CTRL|'B',    SPEC|'L'},
  92.     {VK_RIGHT,    CTRL|'F',    SPEC|'R'},
  93.     {VK_UP,        CTRL|'P',    SPEC|'U'},
  94.     {VK_DOWN,    CTRL|'N',    SPEC|'D'},
  95.     {VK_PRIOR,    CTRL|'Z',    CTRL|'Z'},
  96.     {VK_NEXT,    CTRL|'V',    CTRL|'V'},
  97.     {VK_END,    CTRL|'E',    SPEC|'E'},
  98.     {VK_HOME,    CTRL|'A',    SPEC|'H'},
  99.     {VK_INSERT,    CTRL|'O',    CTRL|'O'},
  100.     {VK_DELETE,    CTRL|'D',    CTRL|'D'},
  101.     {VK_F1,        SPEC|'1',    SPEC|'1'},
  102.     {VK_F2,        SPEC|'2',    SPEC|'2'},
  103.     {VK_F3,        SPEC|'3',    SPEC|'3'},
  104.     {VK_F4,        SPEC|'4',    SPEC|'4'},
  105.     {VK_F5,        SPEC|'5',    SPEC|'5'},
  106.     {VK_F6,        SPEC|'6',    SPEC|'6'},
  107.     {VK_F7,        SPEC|'7',    SPEC|'7'},
  108.     {VK_F8,        SPEC|'8',    SPEC|'8'},
  109.     {VK_F9,        SPEC|'9',    SPEC|'9'},
  110.     {VK_F10,    SPEC|'0',    SPEC|'0'},
  111.     {VK_F11,    SPEC|':',    SPEC|':'},
  112.     {VK_F12,    SPEC|';',    SPEC|';'},
  113. };
  114.  
  115. static int obIndex = 0;
  116.  
  117. static HANDLE hStdInp = INVALID_HANDLE_VALUE;
  118. static HANDLE hStdOut = INVALID_HANDLE_VALUE;
  119.  
  120. static char OutBuf[OBUFSIZE];
  121.  
  122. static DWORD InpConMode;
  123. static DWORD OutConMode;
  124.  
  125. static CONSOLE_CURSOR_INFO OutCurInfo;
  126.  
  127. VOID PASCAL NEAR ttopen(VOID)            // Open the console
  128. {
  129.     CONSOLE_CURSOR_INFO curInfo;
  130.     CONSOLE_SCREEN_BUFFER_INFO conInfo;
  131.  
  132.         hStdInp = GetStdHandle(STD_INPUT_HANDLE);
  133.         hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  134.  
  135.         GetConsoleMode(hStdInp, &InpConMode);
  136.         GetConsoleMode(hStdOut, &OutConMode);
  137.  
  138.         GetConsoleCursorInfo(hStdOut, &OutCurInfo);
  139.  
  140.         SetConsoleMode(hStdInp, ENABLE_WINDOW_INPUT);
  141.         SetConsoleMode(hStdOut, ENABLE_PROCESSED_OUTPUT);
  142.  
  143.         curInfo.dwSize = 20; // thin cursor
  144.         curInfo.bVisible = TRUE;
  145.  
  146.         SetConsoleCursorInfo(hStdOut, &curInfo);
  147.  
  148.         GetConsoleScreenBufferInfo(hStdOut, &conInfo);
  149.  
  150.         term.t_ncol = conInfo.dwSize.X;
  151.         term.t_nrow = conInfo.dwSize.Y-1;
  152. }
  153.  
  154. VOID PASCAL NEAR ttclose(VOID)            // Close the console
  155. {
  156.         SetConsoleMode(hStdInp, InpConMode);
  157.         SetConsoleMode(hStdOut, OutConMode);
  158.  
  159.         SetConsoleCursorInfo(hStdOut, &OutCurInfo);
  160.  
  161.         hStdInp = INVALID_HANDLE_VALUE;
  162.         hStdOut = INVALID_HANDLE_VALUE;
  163. }
  164.  
  165. VOID PASCAL NEAR ttkopen(VOID)            // Open keyboard
  166. {
  167.         // everything is done in one spot; see ttopen()
  168. }
  169.  
  170. VOID PASCAL NEAR ttkclose(VOID)            // Close keyboard
  171. {
  172.         // everything is done in one spot; see ttclose()
  173. }
  174.  
  175. int PASCAL NEAR ttgetc(VOID)            // Get character from console
  176. {
  177.     BOOL loopy=TRUE;
  178.     INPUT_RECORD inRec;
  179.     COORD winSize;
  180.     DWORD dwCount;
  181.     DWORD cKS;
  182.     WORD vKC;
  183.     BOOL ok;
  184.     int i, ch;
  185.  
  186.         ttflush();
  187.  
  188.         while (loopy)
  189.         {
  190.             ok = ReadConsoleInput(hStdInp, &inRec, 1, &dwCount);
  191.  
  192.             if (ok && dwCount)
  193.             {
  194.                 switch (inRec.EventType)
  195.                 {
  196.                     case WINDOW_BUFFER_SIZE_EVENT:
  197.  
  198.                         winSize = inRec.Event.WindowBufferSizeEvent.dwSize;
  199.  
  200.                         newwidth(TRUE, winSize.X);
  201.                         newsize(TRUE, winSize.Y);
  202.  
  203.                         ch = (CTRL | 'L');    // force refresh
  204.  
  205.                         loopy = FALSE;
  206.  
  207.                         break;
  208.  
  209.                     case KEY_EVENT:
  210.  
  211.                         if (inRec.Event.KeyEvent.bKeyDown)
  212.                         {
  213.                             ch = inRec.Event.KeyEvent.uChar.AsciiChar;
  214.                             cKS = inRec.Event.KeyEvent.dwControlKeyState;
  215.                             vKC = inRec.Event.KeyEvent.wVirtualKeyCode;
  216.  
  217.                             if (ch)
  218.                             {
  219.                                 if (cKS & (LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED))
  220.                                 {
  221.                                     ch |= ALTD;
  222.                                 }
  223.  
  224.                                 loopy = FALSE;
  225.                             }
  226.                             else
  227.                             {
  228.                                 for (i=0; i<KEYTBLSIZE; i++)
  229.                                 {
  230.                                     if (KeyTbl[i][0] == vKC)
  231.                                     {
  232.                                         if (cKS & (LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED))
  233.                                             ch = KeyTbl[i][2];
  234.                                         else
  235.                                             ch = KeyTbl[i][1];
  236.  
  237.                                         if (ch) loopy = FALSE;
  238.  
  239.                                         break;
  240.                                     }
  241.                                 }
  242.                             }
  243.                         }
  244.  
  245.                         break;
  246.                 }
  247.             }
  248.         }
  249.  
  250.         return (ch);
  251. }
  252.  
  253. VOID PASCAL NEAR ttputc(int ch)            // Put character into output buffer
  254. {
  255.         OutBuf[obIndex++] = ch;
  256.  
  257.         if (obIndex >= OBUFSIZE)
  258.         {
  259.             ttflush();
  260.         }
  261. }
  262.  
  263. VOID PASCAL NEAR ttflush(VOID)            // Flush console output buffer
  264. {
  265.     DWORD cWrite;
  266.  
  267.         if (obIndex)
  268.         {
  269.             WriteConsole(hStdOut, &OutBuf, obIndex, &cWrite, NULL);
  270.  
  271.             obIndex = 0;
  272.         }
  273. }
  274.  
  275. VOID PASCAL NEAR ttmove(int r, int c)    // Move to row and column
  276. {
  277.     COORD xyCursor;
  278.  
  279.         ttflush();
  280.  
  281.         xyCursor.X = c;
  282.         xyCursor.Y = r;
  283.  
  284.         SetConsoleCursorPosition(hStdOut, xyCursor);
  285. }
  286.  
  287. VOID PASCAL NEAR tteeol(VOID)            // Clear to end of line
  288. {
  289.     const TCHAR fillChar = ' ';
  290.     const WORD Attrib = (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  291.     CONSOLE_SCREEN_BUFFER_INFO conInfo;
  292.     DWORD cWrite;
  293.     int cBlank;
  294.  
  295.         GetConsoleScreenBufferInfo(hStdOut, &conInfo);
  296.  
  297.         cBlank = conInfo.dwSize.X - conInfo.dwCursorPosition.X;
  298.  
  299.         FillConsoleOutputCharacter(hStdOut, fillChar, cBlank, conInfo.dwCursorPosition, &cWrite);
  300.  
  301.         FillConsoleOutputAttribute(hStdOut, Attrib, cBlank, conInfo.dwCursorPosition, &cWrite);
  302. }
  303.  
  304. VOID PASCAL NEAR tteeop(VOID)            // Clear to end of page
  305. {
  306.     const TCHAR fillChar = ' ';
  307.     const WORD Attrib = (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  308.     CONSOLE_SCREEN_BUFFER_INFO conInfo;
  309.     DWORD cWrite;
  310.     int cBlank;
  311.  
  312.         GetConsoleScreenBufferInfo(hStdOut, &conInfo);
  313.  
  314.         cBlank = conInfo.dwSize.X * conInfo.dwSize.Y;
  315.  
  316.         FillConsoleOutputCharacter(hStdOut, fillChar, cBlank, conInfo.dwCursorPosition, &cWrite);
  317.  
  318.         FillConsoleOutputAttribute(hStdOut, Attrib, cBlank, conInfo.dwCursorPosition, &cWrite);
  319. }
  320.  
  321. VOID PASCAL NEAR ttbeep(VOID)            // Beep the speaker
  322. {
  323.         Beep(750, 250);
  324. }
  325.  
  326. VOID PASCAL NEAR ttrev(int status)        // Set reverse status
  327. {
  328.     WORD Attribs;
  329.  
  330.         ttflush();
  331.  
  332.         if (status)
  333.             Attribs = (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
  334.         else
  335.             Attribs = (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
  336.  
  337.         SetConsoleTextAttribute(hStdOut, Attribs);
  338. }
  339.  
  340. int PASCAL NEAR ttrez(int rez)            // Change screen resolution
  341. {
  342.         return (FALSE);
  343. }
  344.  
  345. #if COLOR
  346.  
  347. VOID PASCAL NEAR ttfcol(int color)        // Set foreground color
  348. {
  349. }
  350.  
  351. VOID PASCAL NEAR ttbcol(int color)        // Set background color
  352. {
  353. }
  354.  
  355. #endif
  356.  
  357. #if TYPEAH
  358.  
  359. int PASCAL NEAR typahead(VOID)            // Check for type-ahead
  360. {
  361. //    DWORD cEvents;
  362. //
  363. //    Dosen't work...may need to see only keyboard events.
  364. //
  365. //        GetNumberOfConsoleInputEvents(hStdInp, &cEvents);
  366. //
  367. //        return (cEvents);
  368.  
  369.         return (0);
  370. }
  371.  
  372. #endif
  373.  
  374. int PASCAL NEAR spal(char *pstr)        // Change palette string
  375. {
  376.         return (0);
  377. }
  378.  
  379. #if FLABEL
  380.  
  381. int PASCAL NEAR fnclabel(int f, int n)    // Label a function key
  382. {
  383.         return (0);
  384. }
  385.  
  386. #endif
  387.  
  388. int PASCAL NEAR execprg(int f, int n)    // Not done...
  389. {
  390.         return (FALSE);
  391. }
  392.  
  393. int PASCAL NEAR filter(int f, int n)    // Not done...
  394. {
  395.         return (FALSE);
  396. }
  397.  
  398. int PASCAL NEAR pipecmd(int f, int n)    // Not done...
  399. {
  400.         return (FALSE);
  401. }
  402.  
  403. int PASCAL NEAR spawn(int f, int n)        // Not done...
  404. {
  405.         return (FALSE);
  406. }
  407.  
  408. int PASCAL NEAR spawncli(int f, int n)    // Not done...
  409. {
  410.         return (FALSE);
  411. }
  412.  
  413. char *PASCAL NEAR timeset(VOID)            // Not done...
  414. {
  415.         return (NULL);
  416. }
  417.  
  418. char *PASCAL NEAR getffile(char *fspec)    // Not done...
  419. {
  420.         return (NULL);
  421. }
  422.  
  423. char *PASCAL NEAR getnfile(VOID)        // Not done...
  424. {
  425.         return (NULL);
  426. }
  427.  
  428. #endif
  429.